package com.bear.newsset.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import com.bear.newsset.util.ADCheckUtil;
import com.bear.newsset.util.LogUtils;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import static com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm.NARROW_COLUMNS;

/**
 * 重写WebView
 * 作用：手机端正常显示，而不是PC端的网页
 * 提示：加载前会隐藏，加载完成后才会显示
 */
public class ContentWebView extends WebView {
    public interface ShouldOverrideUrlLoadingListener {
        void url(WebView v, String url);
    }

    private boolean enableDefaultTouchLoad = true;
    private ShouldOverrideUrlLoadingListener mShouldOverrideUrlLoadingListener;

    public ContentWebView(Context context) {
        super(context);
        init();
    }

    public ContentWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 初始化WebView
     */
    @SuppressLint("SetJavaScriptEnabled")
    private void init() {
        setVisibility(View.INVISIBLE);              //加载前隐藏
        WebSettings webSet = getSettings();
        webSet.setJavaScriptEnabled(true);          //开启JavaScript
        webSet.setLayoutAlgorithm(NARROW_COLUMNS);  //适应屏幕，内容将自动缩放
        webSet.setLoadWithOverviewMode(true);       //缩放至屏幕大小
        webSet.setUseWideViewPort(true);
        setWebViewClient(new WebViewClient() {
            /**
             * 点击链接事
             * @param view      WebView
             * @param url       链接
             * @return          是否响应
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogUtils.d("shouldOverrideUrlLoading", url);
                if( mShouldOverrideUrlLoadingListener != null ) {
                    mShouldOverrideUrlLoadingListener.url(view, url);
                }
                //加载点击的链接
                if( enableDefaultTouchLoad ) view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                if( getVisibility() != View.VISIBLE ) {
                    setVisibility(View.VISIBLE);//加载完毕后显示
                }
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, String s) {
                LogUtils.d("shouldInterceptRequest", s);
                //拦截广告
                WebResourceResponse wrr = ADCheckUtil.interceptADResponseForX5Core(webView, s);
                return wrr != null ? wrr : super.shouldInterceptRequest(webView, s);
            }
        });//重写Client
    }

    /**
     * 监听重写Url加载
     * @param listener  监听器
     */
    public void setShouldOverrideUrlLoadingListener(ShouldOverrideUrlLoadingListener listener) {
        mShouldOverrideUrlLoadingListener = listener;
    }

    /**
     * 是否启用默认的点击加载
     * 默认启用，启用后，当点击Url后会在当前WebView中打开
     * @param enable    是否启用
     */
    public void setEnableDefaultTouchLoad(boolean enable) {
        enableDefaultTouchLoad = enable;
    }
}
